<?php
class PostController extends Controller {
	public function __construct(){
		parent::__construct();
		if($this->router->action!='login'){
			$this->islogin();
			$this->isaccess();
			$l = $this->model('Logs');
			$l->savelog();
		}
		$this->helper(array('cookie'));
	}

	public function addprivilegecate(){
		$pri = $this->model('Privilege');
		$pri->addprivilegecate();
		redirect('admin/privilege');
	}

	public function addprivilege() {
		$pri = $this->model('Privilege');
		$pri->addprivilege();
		redirect('admin/privilege');
	}

	public function updateprivilege() {
		$pri = $this->model('Privilege');
		$pri->updateprivilege();
		redirect('admin/privilege');
	}

	public function delprivilege() {
		$pri = $this->model('Privilege');
		$pri->delprivilege();
		redirect('admin/privilege');
	}

	public function savediy() {
		$m = $this->model('Menu');
		$m->savediy();
		redirect('admin/setdiy');

	}
	public function addrole() {
		$r = $this->model('Role');
		$r->addrole();
		redirect('admin/role');
	}
	public function updaterole() {
		$r = $this->model('Role');
		$r->updaterole();
		redirect('admin/role');
	}
	public function delrole() {
		$r = $this->model('Role');
		$r->delrole();
		redirect('admin/role');
	}
	public function assignrole() {
		$r = $this->model('Role');
		$r->assignrole();
		redirect('admin/role');
	}
	public function adduser() {
		$u = $this->model('User');
		if($_POST['u_pass']==$_POST['u_pass2'] && empty($_POST['u_pass'])){
			$u->adduser();
			redirect('admin/users');
		}else{
			$msginfo = array (
				'msg' => '两次输入的密码不一致，请重新输入！',
			);
			$this->assign($msginfo);
			$this->show('admin/message.php');
		}
	}
	public function updateuser() {
		$u = $this->model('User');
		if($_POST['u_pass']==$_POST['u_pass2'] && !empty($_POST['u_pass'])){
			$u->updateuser($_POST['u_id']);
			redirect('admin/edituser/uid/'.$_POST['u_id']);
		}else{
			$msginfo = array (
				'msg' => '两次输入的密码不一致，请重新输入！',
			);
			$this->assign($msginfo);
			$this->show('admin/message.php');
		}
	}
	public function updateprofile() {
		$u = $this->model('User');
		if($_POST['u_pass']==$_POST['u_pass2'] && empty($_POST['u_pass'])){
			$u->updateprofile(get_cookie('u_id'));
			redirect('admin/profile');
		}else{
			$msginfo = array (
				'msg' => '两次输入的密码不一致，请重新输入！',
			);
			$this->assign($msginfo);
			$this->show('admin/message.php');
		}
	}
	public function deluser() {
		$u = $this->model('User');
		$u->deluser();
		redirect('admin/users');
	}
	public function login(){
		session_start();
		//$this->referer();
		if($_SESSION['login_check_number']==$_POST['checknumber']){
			$user = $this->model('User');
			if($user->login()){
				redirect('admin/main');
			}else{
				$msginfo = array (
				'msg' => '用户名或密码错误！',
				'pageurl' => 'admin/index'
				);
				$this->assign($msginfo);
				$this->show('message.php');
			}
		}else{
			$msginfo = array (
				'msg' => '验证码错误！',
				'pageurl' => 'admin/index'
				);
				$this->assign($msginfo);
				$this->show('message.php');
		}
	}
	public function logout(){
		$user = $this->model('User');
		if($user->logout()){
			$msginfo = array (
            'msg' => '成功退出管理系统！',
            'pageurl' => 'admin/index'
            );
            $this->assign($msginfo);
            $this->show('message.php');
		}
	}
	private function referer() {
		$referer = parse_url($_SERVER['HTTP_REFERER']);
		if($referer['host']!='www.sailulu.cn' && $referer['host']!='sailulu.cn'){
			$this->assign('msg','Sorry,Please do not cross-site attack!');
			$this->show('message.php');
			exit;
		}
	}
}